#include "stdafx.h"
#include "channel.h"

vector<int> channel_divide(){
	vector <int>ans;
	vector <int>temp;
	for(int i=0;i<CHANNEL_NUM;i++)
		temp.push_back(i);
	for(int i=0;i<CHANNEL_NUM;i++){
		int position=rand()%temp.size();
		ans.push_back(temp.at(position));
		temp.erase(temp.begin()+position);
	}
	return ans;
}
void print(double* pData, int count){  
	for (int i = 0; i< count; i++) {  
		cout << pData[i] << " ";  
	}  
	cout << endl;  
}  
void BubbleSort(double* pData, int count)  
{  
	int temp;  
	for (int i = 1; i < count; i++)  
	{  
		for (int j = count - 1; j >= i; j--)  
		{  
			if (pData[j] < pData[j - 1])  
			{  
				temp = pData[j - 1];  
				pData[j - 1] = pData[j];  
				pData[j] = temp;  
			}  
		}  
		cout << "The "<< i <<" round:" << endl;  
		print(pData, count);  
		cout << "----------------------------" << endl;  
	}  
}  

void Matrix_Hessenberg(int *A1,int n,double *ret)
{
	int i,j,k,MaxNumber;
	double temp,*A;
	A=new double[n*n];
	for (i=0;i<n;i++)
	{
		k=i*n;
		for (j=0;j<n;j++)
		{
			A[k+j]=A1[k+j];
		}
	}
	for (k=1;k<n-1;k++)
	{
		i=k-1;
		MaxNumber=k;
		temp=abs(A[k*n+i]);
		for (j=k+1;j<n;j++)
		{
			if (abs(A[j*n+i])>temp)
			{
				temp=abs(A[j*n+i]);
				MaxNumber=j;
			}
		}
		ret[0]=A[MaxNumber*n+i];
		i=MaxNumber;
		if (ret[0]!=0)
		{
			if (i!=k)
			{
				for(j=k-1;j<n;j++)
				{
					temp=A[i*n+j];
					A[i*n+j]=A[k*n+j];
					A[k*n+j]=temp;
				}
				for(j=0;j<n;j++)
				{
					temp=A[j*n+i];
					A[j*n+i]=A[j*n+k];
					A[j*n+k]=temp;
				}
			}
			for (i=k+1;i<n;i++)
			{
				temp=A[i*n+k-1]/ret[0];
				A[i*n+k-1]=0;
				for (j=k;j<n;j++)
				{
					A[i*n+j]-=temp*A[k*n+j];
				}
				for (j=0;j<n;j++)
				{
					A[j*n+k]+=temp*A[j*n+i];
				}
			}
		}

	}
	for (i=0;i<n;i++)
	{
		k=i*n;
		for (j=0;j<n;j++)
		{
			ret[k+j]=A[k+j];
		}
	}
	delete []A;
}
bool Matrix_EigenValue(int *K1,int n,int LoopNumber,double Error1,double *Ret)
{
	int i,j,k,t,m,Loop1;
	double b,c,d,g,xy,p,q,r,x,s,e,f,z,y,temp,*A;
	A=new double[n*n];
	Matrix_Hessenberg(K1,n,A);
	m=n;
	Loop1=LoopNumber;
	while(m!=0)
	{
		t=m-1;
		while(t>0)
		{
			temp=abs(A[(t-1)*n+t-1]);
			temp+=abs(A[t*n+t]);
			temp=temp*Error1;
			if (abs(A[t*n+t-1])>temp)
			{
				t--;
			}
			else
			{
				break;
			}
		}
		if (t==m-1)
		{
			Ret[(m-1)*2]=A[(m-1)*n+m-1];
			Ret[(m-1)*2+1]=0;
			m-=1;
			Loop1=LoopNumber;
		}
		else if(t==m-2)
		{
			b=-A[(m-1)*n+m-1]-A[(m-2)*n+m-2];
			c=A[(m-1)*n+m-1]*A[(m-2)*n+m-2]-A[(m-1)*n+m-2]*A[(m-2)*n+m-1];
			d=b*b-4*c;
			y=sqrt(abs(d));
			if (d>0)
			{
				xy=1;
				if (b<0)
				{
					xy=-1;
				}
				Ret[(m-1)*2]=-(b+xy*y)/2;
				Ret[(m-1)*2+1]=0;
				Ret[(m-2)*2]=c/Ret[(m-1)*2];
				Ret[(m-2)*2+1]=0;
			}
			else
			{
				Ret[(m-1)*2]=-b/2;
				Ret[(m-2)*2]=-b/2;
				Ret[(m-1)*2+1]=y/2;
				Ret[(m-2)*2+1]=-y/2;
			}
			m-=2;
			Loop1=LoopNumber;
		}
		else
		{
			if (Loop1<1)
			{
				return false;
			}
			Loop1--;
			j=t+2;
			while (j<m)
			{
				A[j*n+j-2]=0;
				j++;
			}
			j=t+3;
			while (j<m)
			{
				A[j*n+j-3]=0;
				j++;
			}
			k=t;
			while (k<m-1)
			{
				if (k!=t)
				{
					p=A[k*n+k-1];
					q=A[(k+1)*n+k-1];
					if (k!=m-2)
					{
						r=A[(k+2)*n+k-1];
					}
					else
					{
						r=0;
					}
				}
				else
				{
					b=A[(m-1)*n+m-1];
					c=A[(m-2)*n+m-2];
					x=b+c;
					y=b*c-A[(m-2)*n+m-1]*A[(m-1)*n+m-2];
					p=A[t*n+t]*(A[t*n+t]-x)+A[t*n+t+1]*A[(t+1)*n+t]+y;
					q=A[(t+1)*n+t]*(A[t*n+t]+A[(t+1)*n+t+1]-x);
					r=A[(t+1)*n+t]*A[(t+2)*n+t+1];
				}
				if (p!=0 || q!=0 || r!=0)
				{
					if (p<0)
					{
						xy=-1;
					}
					else
					{
						xy=1;
					}
					s=xy*sqrt(p*p+q*q+r*r);
					if (k!=t)
					{
						A[k*n+k-1]=-s;
					}
					e=-q/s;
					f=-r/s;
					x=-p/s;
					y=-x-f*r/(p+s);
					g=e*r/(p+s);
					z=-x-e*q/(p+s);
					for (j=k;j<m;j++)
					{
						b=A[k*n+j];
						c=A[(k+1)*n+j];
						p=x*b+e*c;
						q=e*b+y*c;
						r=f*b+g*c;
						if (k!=m-2)
						{
							b=A[(k+2)*n+j];
							p+=f*b;
							q+=g*b;
							r+=z*b;
							A[(k+2)*n+j]=r;
						}
						A[(k+1)*n+j]=q;
						A[k*n+j]=p;
					}
					j=k+3;
					if (j>m-2)
					{
						j=m-1;
					}
					for (i=t;i<j+1;i++)
					{
						b=A[i*n+k];
						c=A[i*n+k+1];
						p=x*b+e*c;
						q=e*b+y*c;
						r=f*b+g*c;
						if (k!=m-2)
						{
							b=A[i*n+k+2];
							p+=f*b;
							q+=g*b;
							r+=z*b;
							A[i*n+k+2]=r;
						}
						A[i*n+k+1]=q;
						A[i*n+k]=p;
					}
				}
				k++;
			}
		}
	}
	delete []A;
	return true;
}

